﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Services;
using GestionPedago.Services.ES;
using System.Windows.Input;
using System.Windows;

namespace GestionPedago.ViewModel
{
    /// <summary>
    /// Classe qui représente l'onglet de planification des ECF par promotion
    /// </summary>
    public class PlanifierECFPromotionVM : OngletVM
    {

        private ICommand savePlaningsCommand;

        public ICommand SavePlaningsCommand
        {
            get
            {
                if (this.savePlaningsCommand == null)
                    this.savePlaningsCommand = new RelayCommand(param => this.EnregistrerPlannings());

                return this.savePlaningsCommand;
            }
        }

        /// <summary>
        /// Action d'enregistrement des planifications d'ECF validés
        /// </summary>
        public void EnregistrerPlannings()
        {
            if (MessageBox.Show("Voulez vous enregistrer les planifications d'ECF validé ?", "Enregistrer", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                try
                {
                    Parent.vmMainWindow.vmBarreInfos.Message = "Enregistrement en cours ...";

                    foreach (PlanningECFES pe in ECFs)
                    {
                        if (pe.CodeContactENI != null)
                        {
                            pe.ContactENI = Correcteurs.Single(c => c.CodeContactENI == pe.CodeContactENI);
                        }
                    }
                    ECFs = new ObservableCollection<PlanningECFES>(FacadeServices.GetInstance().PlanifierPlanningECF(new PlanifierPlanningECFIn(ECFs.ToList(), Promotion)).ListePlanningECF);

                    if(ListPlanningECFDetail != null) {
                        ListPlanningECFDetail.Clear();
                    }

                    Parent.RefreshAfterPlanificationsPromo();

                    Parent.vmMainWindow.vmBarreInfos.Message = "Enregistrement correctement effectué.";

                }
                catch (Exception e)
                {
                    MessageBox.Show("Erreur lors de l'enregistrement :\n" + e.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }


        private object pSelectedECF;
        public object SelectedECF {
            get { return pSelectedECF; }
            set {
                pSelectedECF = value;
                if(pSelectedECF != null) {
                    PlanningECFES plECF = pSelectedECF as PlanningECFES;
                    ListPlanningECFDetail = new ObservableCollection<PlanningECFDetailES>(FacadeServices.GetInstance().RecupererPlanningECFDetailByPlanningECF(new RecupererPlanningECFDetailByPlanningECFIn(plECF)).PlanningECFDetail);
                }
                RaisePropertyChanged("SelectedECF");
            }
        }

        /// <summary>
        /// Liste de toutes les promotions affichées dans la liste déroulante
        /// </summary>
        public List<PromotionES> Promotions {
            get { return FacadeServices.GetInstance().RecupererPromotions(new RecupererPromotionsIn(true)).ListePromotions; }
        }
        private PromotionES pPromotion;
        /// <summary>
        /// Promotion séléctionnée dans la liste déroulante
        /// </summary>
        public PromotionES Promotion 
        { 
            get { return pPromotion; }
            set 
            {
                if(value != null) 
                {
                    pPromotion = value;
                    ECFs = new ObservableCollection<PlanningECFES>(FacadeServices.GetInstance().RecupererPlanningECF(new Services.ES.RecupererPlanningECFIn(pPromotion)).ListePlanningECF);
                    RaisePropertyChanged("Promotion");
                }
                    
            }
        }

        private ObservableCollection<PlanningECFES> pECFs;
        /// <summary>
        /// Liste des planification d'ECF pour la promotion sélectionnée
        /// </summary>
        public ObservableCollection<PlanningECFES> ECFs 
        {
            get 
            {
                if(pECFs == null && this.Promotion != null) {
                    pECFs = new ObservableCollection<PlanningECFES>(FacadeServices.GetInstance().RecupererPlanningECF(new RecupererPlanningECFIn(this.Promotion)).ListePlanningECF);
                }

                return pECFs;


            }
            set 
            {
                pECFs = value;
                RaisePropertyChanged("ECFs");
            }
        }


        private ObservableCollection<PlanningECFDetailES> pListPlanningECFDetail;
        /// <summary>
        /// Liste des Planning ECF détail pour afficher les liste des cours
        /// </summary>
        public ObservableCollection<PlanningECFDetailES> ListPlanningECFDetail
        {
            get { return pListPlanningECFDetail; }
            set
            {
                pListPlanningECFDetail = value;
                RaisePropertyChanged("ListPlanningECFDetail");
            }
        }

        private ObservableCollection<ContactENIES> pCorrecteurs;
        /// <summary>
        /// Liste des correcteurs affiché dans la liste déroulante
        /// </summary>
        public ObservableCollection<ContactENIES> Correcteurs
        {
            get { return pCorrecteurs; }
            set
            {
                pCorrecteurs = value;
                RaisePropertyChanged("Correcteurs");
            }
        }

        /// <summary>
        /// Constructeur de la classe PlanifierECFPromotionVM
        /// </summary>
        /// <param name="parent">Gestionnaire d'onglets</param>
        public PlanifierECFPromotionVM(GestionnaireOngletsVM parent)
        {
            base.DisplayName = "Planifier les ECF par promotion";
            base.Parent = parent;
            Correcteurs = new ObservableCollection<ContactENIES>(FacadeServices.GetInstance().RecupererFormateurs(new RecupererFormateursIn()).ListeFormateurs);
        }


    }
}
